package com.genntii.examManager.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.genntii.examManager.domain.entity.ExamRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.ArrayList;

@Mapper
public interface ExamRecordMapper extends BaseMapper<ExamRecord> {

    @Select("select * from exam_record where user_id = #{userId} and exam_id = #{examId}")
    ExamRecord getExamRecordByExamUserId(@Param("examId") Long examId,@Param("userId") Long userId);

    @Select("select * " +
            "from exam_record " +
            "where user_id = #{userId} " +
            "order by commit_time DESC " +
            "limit #{pageNum}, #{pageSize}")
    ArrayList<ExamRecord> getExamHistory(@Param("userId") Long userId,
                                         @Param("pageNum") Integer pageNum,
                                         @Param("pageSize") Integer pageSize);

    @Select("select count(*) from exam_record where user_id = #{userId}")
    long getExamHistoryTotal(@Param("userId") Long userId);

    void insertBatch(@Param("examId") Long examId,
                     @Param("userIds") ArrayList<Long> userIds);


    @Select("select * from exam_record where exam_id = #{examId} limit #{pageNum},#{pageSize}")
    ArrayList<ExamRecord> getExamRecordList(@Param("examId") Long examId,
                                            @Param("pageNum") Integer pageNum,
                                            @Param("pageSize") Integer pageSize);


    @Select("select count(*) from exam_record where user_id = #{userId} and (type = 1 or type = 2)")
    Integer getExamCountUserCompleted(@Param("userId") Long userId);

    @Select("select * from exam_record where user_id = #{userId} order by commit_time desc limit 15")
    ArrayList<ExamRecord> getExamHistory15(@Param("userId") Long userId);

    @Select("select * from exam_record where exam_id = #{examId} and type = 2")
    ArrayList<ExamRecord> getAllByExamId(@Param("examId") Long examId);

    @Select("select * from exam_record where user_id = #{userId} and exam_id = #{examId}")
    ExamRecord getExamRecordByUserId(@Param("userId") Long userId,
                                     @Param("examId") Long examId);

    @Select("select * from exam_record where user_id = #{userId} and exam_id = #{examId}")
    ExamRecord getExamHistoryDetial(@Param("userId") Long userId,
                              @Param("examId") Long examId);

    ArrayList<ExamRecord> getExamRecordListByUserExamId(@Param("pageNum") Integer pageNum,
                                                        @Param("pageSize") Integer pageSize,
                                                        @Param("examIds") ArrayList<Long> examIds,
                                                        @Param("userId") Long userId);

    void insertBatchAll(ArrayList<ExamRecord> examRecords);

    @Select("select * " +
            "from exam_record " +
            "where exam_id = #{examId} " +
            "limit #{pageNum}, #{pageSize}")
    ArrayList<ExamRecord> getExamRecordListPage(@Param("pageNum") int pageNum,
                                                @Param("pageSize") Integer pageSize,
                                                @Param("examId") Long examId);

    @Select("select count(*) " +
            "from exam_record " +
            "where exam_id = #{examId} ")
    long getExamRecordCount(@Param("examId") Long examId);

    @Select("select * from exam_record where exam_id = #{examId}")
    ArrayList<ExamRecord> getExamRecordListForeach(Long examId);

    @Select("select count(*) " +
            "from exam_manager.exam_record " +
            "where type = 2 and exam_id = #{examId}")
    int getCompleteNum(@Param("examId") Long examId);

    @Select("select count(*) " +
            "from exam_manager.exam_record " +
            "where type = 1 and exam_id = #{examId}")
    int getWaitGrading(@Param("examId") Long examId);

    @Select("select count(*) " +
            "from exam_manager.exam_record " +
            "where type = 0 and exam_id = #{examId}")
    int getNoCompleteNum(@Param("examId") Long examId);

    void deleteByUserIdAndExamIds(Long userId, ArrayList<Long> examIds);
}